package net.ibizsys.eclipse.studio.xml;

import org.w3c.dom.Node;

import net.ibizsys.eclipse.studio.util.StringKit;

/**
 * 实体
 * 
 * @author <a href='http://www.weibo.com/xjtoo' target='_blank'>Enmaai</a>
 */
public class IBizEntity extends IBizSearchableNode
{
	public static final String NODE_PSDATAENTITY = "PSDATAENTITY";

	/**
	 * 代码名称
	 */
	public static final String ATTR_CODENAME = "CODENAME";
	/**
	 * 逻辑名称
	 */
	public static final String ATTR_LOGICNAME = "LOGICNAME";
	/**
	 * 模块标识
	 */
	public static final String ATTR_PSMODULEID = "PSMODULEID";

	private String codeName;
	private String logicName;
	private String moduleId;

	private IBizDELogicList logicList = new IBizDELogicList();
	private IBizDEActionList actionList = new IBizDEActionList();
	private IBizDEACModeList acmodeList = new IBizDEACModeList();
	private IBizDatasetList datasetList = new IBizDatasetList();
	private IBizDataQueryList dataqueryList = new IBizDataQueryList();
	private IBizDEPrintList printList = new IBizDEPrintList();
	private IBizDEReportList reportList = new IBizDEReportList();
	private IBizDEWFList wfList = new IBizDEWFList();
	private IBizDEUIActionList uiactionList = new IBizDEUIActionList();

	@Override
	protected void initAttribute(String name, String value)
	{
		if (StringKit.IsEqual(ATTR_CODENAME, name, true))
		{
			this.codeName = value;
			return;
		}
		if (StringKit.IsEqual(ATTR_LOGICNAME, name, true))
		{
			this.logicName = value;
			return;
		}
		if (StringKit.IsEqual(ATTR_PSMODULEID, name, true))
		{
			this.moduleId = value;
			return;
		}
		super.initAttribute(name, value);
	}

	@Override
	protected void initChildNode(Node node, IBizProject project)
	{
		if (StringKit.IsEqual(node.getNodeName(), IBizDELogicList.NODE_PSDELOGICS, true))
		{
			logicList.init(node, project);
			return;
		}

		if (StringKit.IsEqual(node.getNodeName(), IBizDEActionList.NODE_PSDEACTIONS, true))
		{
			actionList.init(node, project);
			return;
		}

		if (StringKit.IsEqual(node.getNodeName(), IBizDEACModeList.NODE_PSDEACMODES, true))
		{
			acmodeList.init(node, project);
			return;
		}

		if (StringKit.IsEqual(node.getNodeName(), IBizDatasetList.NODE_PSDEDATASETS, true))
		{
			datasetList.init(node, project);
			return;
		}

		if (StringKit.IsEqual(node.getNodeName(), IBizDataQueryList.NODE_PSDEDATAQUERYIES, true))
		{
			dataqueryList.init(node, project);
			return;
		}

		if (StringKit.IsEqual(node.getNodeName(), IBizDEPrintList.NODE_PSDEPRINTS, true))
		{
			printList.init(node, project);
			return;
		}

		if (StringKit.IsEqual(node.getNodeName(), IBizDEReportList.NODE_PSDEREPORTS, true))
		{
			reportList.init(node, project);
			return;
		}

		if (StringKit.IsEqual(node.getNodeName(), IBizDEWFList.NODE_PSDEWFS, true))
		{
			wfList.init(node, project);
			return;
		}
		if (StringKit.IsEqual(node.getNodeName(), IBizDEUIActionList.NODE_PSDEUIACTIONS, true))
		{
			uiactionList.init(node, project);
			return;
		}
		super.initChildNode(node, project);
	}

	public String getCodeName()
	{
		return codeName;
	}

	public String getLogicName()
	{
		return logicName;
	}

	public String getModuleId()
	{
		return moduleId;
	}

	public IBizDELogicList getLogicList()
	{
		return logicList;
	}

	public IBizDEActionList getActionList()
	{
		return actionList;
	}

	public IBizDEACModeList getAcmodeList()
	{
		return acmodeList;
	}

	public IBizDatasetList getDatasetList()
	{
		return datasetList;
	}

	public IBizDataQueryList getDataqueryList()
	{
		return dataqueryList;
	}

	public IBizDEPrintList getPrintList()
	{
		return printList;
	}

	public IBizDEReportList getReportList()
	{
		return reportList;
	}

	public IBizDEWFList getWfList()
	{
		return wfList;
	}

	public IBizDEUIActionList getUIActionList()
	{
		return uiactionList;
	}

	private String getModuleName()
	{
		if (getProject() != null)
		{
			IBizModule module = getProject().getModuleList().getItemById(moduleId);

			if (module != null)
			{
				return module.getName();
			}
		}
		return moduleId;
	}

	@Override
	public String getText()
	{
		if (StringKit.IsBlank(moduleId))
		{
			return String.format("[%1$s]", getName());
		} else
		{
			return String.format("[%1$s][%2$s]", getModuleName(), getName());
		}
	}

	@Override
	public String getDesc()
	{
		return getLogicName();
	}

	@Override
	public String getIcon()
	{
		return "icons/entity.png";
	}

	@Override
	public boolean match(String keyword)
	{
		if (StringKit.has(getId(), keyword) || StringKit.has(getName(), keyword) || StringKit.has(getCodeName(), keyword) || StringKit.has(getLogicName(), keyword))
		{
			return true;
		}
		return false;
	}
}
